package com.santiao.fuel.ui.fragment

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.santiao.fuel.R
import com.santiao.fuel.SplashActivity
import com.santiao.fuel.bean.RYChargeListBean
import com.santiao.fuel.event.XXLEvent
import com.santiao.fuel.ui.activity.CalculateWaterActivity
import com.santiao.fuel.ui.activity.RYContentWebViewActivity
import com.santiao.fuel.ui.activity.RYWaterSelectedActivity
import com.santiao.fuel.ui.adapter.ChargeListAdapter
import com.santiao.fuel.ui.adapter.base.ItemClickListener
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class WaterFragment : Fragment(){

    private lateinit var charge_rv_recycle:RecyclerView

    private lateinit var water_rl_price:RelativeLayout
    private lateinit var water_rl_all:RelativeLayout
    private lateinit var charge_tv_selected:TextView

    var chargeData : ArrayList<RYChargeListBean>  = ArrayList()
    private lateinit var water_container: FrameLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_water,container,false)
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        if(!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        initView(view)

    }
    private fun initView(view: View){
        charge_tv_selected = view.findViewById(R.id.charge_tv_selected)
//        if("vivo".equals(SplashActivity.mymanufacturer)){
//            charge_tv_selected.visibility= View.GONE
//        }else{
            charge_tv_selected.visibility= View.VISIBLE
//        }
        charge_rv_recycle = view.findViewById(R.id.charge_rv_recycle)

        water_rl_price = view.findViewById(R.id.water_rl_price)
        water_rl_all = view.findViewById(R.id.water_rl_all)

        water_container = view.findViewById(R.id.water_container)

        charge_tv_selected.setOnClickListener({
//            startActivity(Intent(requireActivity(), RYWaterSelectedActivity::class.java))
            startActivity(Intent(requireActivity(), CalculateWaterActivity::class.java))
        })




        water_rl_price.setOnClickListener({
            var url  = "file:///android_asset/584da72d-5394-4d2c-b87a-dcf6a55cc07c_f_-417287553.html"


            RYContentWebViewActivity.forward(
                requireActivity(),
                url,
                ""
            )
        })

        water_rl_all.setOnClickListener({
            var url  = "file:///android_asset/584da72d-5394-4d2c-b87a-dcf6a55cc07c_f_-1417195842.html"


            RYContentWebViewActivity.forward(
                requireActivity(),
                url,
                ""
            )
        })



        chargeData = ArrayList()
        var chargeItem  = RYChargeListBean()
        chargeItem.iconId = R.drawable.water_function_1
        chargeItem.title = "立即停止喝这种水！世卫组织点名：增加患癌风险，很多人还在天天喝"
        var url  = "file:///android_asset/584da72d-5394-4d2c-b87a-dcf6a55cc07c_f_89902285.html"

        chargeItem.url = url
        chargeData.add(chargeItem)

        chargeItem  = RYChargeListBean()
        chargeItem.iconId = R.drawable.water_function_2
        chargeItem.title = "科普用水常识"
        url  = "file:///android_asset/584da72d-5394-4d2c-b87a-dcf6a55cc07c_f_-15237412141.html"
        chargeItem.url = url
        chargeData.add(chargeItem)


        chargeItem  = RYChargeListBean()
        chargeItem.iconId = R.drawable.water_function_3
        chargeItem.title = "用水科普｜这些用水小常识你知道吗？"
        url  = "file:///android_asset/584da72d-5394-4d2c-b87a-dcf6a55cc07c_f_-1523741214.html"
        chargeItem.url = url
        chargeData.add(chargeItem)

        chargeItem  = RYChargeListBean()
        chargeItem.iconId = R.drawable.water_function_4
        chargeItem.title = "节约用水小妙招"
        url  = "file:///android_asset/584da72d-5394-4d2c-b87a-dcf6a55cc07c_f_-19727658871.html"
        chargeItem.url = url
        chargeData.add(chargeItem)

        chargeItem  = RYChargeListBean()
        chargeItem.iconId = R.drawable.water_function_5
        chargeItem.title = "节约科普｜8个夏日用水小常识"
        url  = "file:///android_asset/584da72d-5394-4d2c-b87a-dcf6a55cc07c_f_-1191293420.html"
        chargeItem.url = url
        chargeData.add(chargeItem)


        charge_rv_recycle.layoutManager= LinearLayoutManager(requireActivity())
        var chargeAdapter=  ChargeListAdapter(requireActivity(),R.layout.item_charge,chargeData)
        charge_rv_recycle.adapter=chargeAdapter
        chargeAdapter.setItemClickListener(object : ItemClickListener<RYChargeListBean> {
            //            override fun onItemClick(
//                parent: AdapterView<*>?,
//                view: View?,
//                position: Int,
//                id: Long
//            ) {
//               ToastUtils.showLong(chargeData.get(position).title)
//            }
            override fun click(position: Int, data: RYChargeListBean) {
                RYContentWebViewActivity.forward(
                    requireActivity(),
                    data.url,
                    ""
                )
            }

        })

    }




    override fun onResume() {
        super.onResume()

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageSimple(t: XXLEvent) {

    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }



}
